package controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import service.DiagnosticianService;

import command.LitiasisCommand;

public class Sintoma3Controller extends SimpleFormController {
	private DiagnosticianService diagnosticianService;
	

	public Sintoma3Controller() {
		this.setCommandClass(LitiasisCommand.class);

	}

	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		response.setHeader("Cache-Control","no-cache");
		
		LitiasisCommand litiasisCommand = (LitiasisCommand) command;

		HttpSession session = request.getSession();
		LitiasisCommand commandInSession = (LitiasisCommand) session.getAttribute("litiasisCommand");
		
		commandInSession.setIrritacionPeritoneal(litiasisCommand.isIrritacionPeritoneal());
		commandInSession.setIctericia(litiasisCommand.isIctericia());
		commandInSession.setColuria(litiasisCommand.isColuria());
		commandInSession.setAcolia(litiasisCommand.isAcolia());

		List<String> diagnosticos = diagnosticianService.diagnose(commandInSession, DiagnosticianService.RULES_3);
		
		session.setAttribute("litiasisCommand", commandInSession);

		ModelAndView modelAndView = super.onSubmit(request, response, command, errors);
		modelAndView.addAllObjects(referenceData(request, command, errors));
		modelAndView.addObject("diagnosticos", diagnosticos);
		return modelAndView;
	}

	public void setDiagnosticianService(DiagnosticianService diagnosticianService) {
		this.diagnosticianService = diagnosticianService;
	}	
}
